package com.bgs.eat.roleBack.controller;

import com.bgs.eat.config.CommonResult;
import com.bgs.eat.page.PageRequest;
import com.bgs.eat.page.PageResult;
import com.bgs.eat.roleBack.entity.RoleBackEntity;
import com.bgs.eat.roleBack.mapper.RoleBackMapper;
import com.bgs.eat.roleBack.service.RoleBackService;
import com.bgs.eat.userBack.entity.UserBackEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName RoleBackController
 * @Description TODO
 * @Author cjl
 * @Date 2022/10/12 8:39
 **/
@RestController
@CrossOrigin
@RequestMapping("roleBack")
public class RoleBackController {

    @Autowired
    RoleBackService roleBackService;

    /**
    * @Author cjl
    * @Description 全查角色（模糊查角色名称）
    * @Date  9:07
    * @Return com.bgs.eat.page.PageResult
    * @param roleName
    * @param pageRequest
    */
    @GetMapping("selectRoleBackEntity")
    PageResult selectRoleBackEntity(String roleName, PageRequest pageRequest){
        PageResult pageResult =roleBackService.selectRoleBackEntity(roleName,pageRequest);
        return pageResult;
    }

    /**
    * @Author cjl
    * @Description 添加或修改角色信息
    * @Date  15:45
    * @Return com.bgs.eat.config.CommonResult
    * @param roleBackEntity
    */
    @PostMapping("saveOrUpdateRoleBackEntity")
    CommonResult saveOrUpdateRoleBackEntity(RoleBackEntity roleBackEntity){
        int i = roleBackService.saveOrUpdateRoleBackEntity(roleBackEntity);
        if (i!=0){
            return CommonResult.ok();
        }else {
            return CommonResult.error("添加或修改失败!!!");
        }
    }

    /**
    * @Author cjl
    * @Description 删除角色信息
    * @Date  15:45
    * @Return java.lang.Integer
    * @param id
    */
    @DeleteMapping("deleteRoleBackEntityById")
    Integer deleteUserBackEntityById(Integer id){
        return roleBackService.deleteRoleBackEntityById(id);
    }

    /**
    * @Author cjl
    * @Description 修改角色的菜单权限
    * @Date  16:45
    * @Return com.bgs.eat.config.CommonResult
    * @param roleBackEntity
    */
    @PostMapping("/assignPermissions")
    public CommonResult assignPermissions (@RequestBody RoleBackEntity roleBackEntity){
        int i = roleBackService.assignPermissions(roleBackEntity);
        return CommonResult.ok(i);
    }

    @GetMapping("roleBackEntityByRoleId")
    CommonResult roleBackEntityByRoleId(){
        List<RoleBackEntity> list = roleBackService.roleBackEntityByRoleId();
        return CommonResult.ok(list);
    }

}
